#import "sprite_game_bridge.h"

#define BRIDGE_DEFAULT_ACTION_TIME 1.0f

@implementation SpriteGameBridge

@synthesize point = point_;
@synthesize flagCover = flagCover_;
@synthesize flagInvincible = flagInvincible_;

- (id)initSpriteGameBridge
{
	if ((self = [super init]))
	{
		point_ = CMgrGameMission::Instance()->start_bridge_point;
		flagInvincible_ = false;

		startPos_ = ccp(-280, 0);
		endPos_ = ccp(320, 0);

		bridge_ = [CCProgressTimer progressWithFile:@"bridge_progress.png"];
		bridgeRed_ = [CCProgressTimer progressWithFile:@"bridge_progress_red.png"];
		bridgeBack_ = [CCSprite spriteWithFile:@"bridge_progress.png"];
		cloud_ = [CCSprite spriteWithSpriteFrameName:@"bg_cloud.png"];
		boy_ = [CCSprite spriteWithSpriteFrameName:@"NPC_boy.png"];
		girl_ = [CCSprite spriteWithSpriteFrameName:@"NPC_girl.png"];
		effectCover_ = [[CCSprite alloc] init];
		effectInvincible_ = [[CCSprite alloc] init];

		[self addChild:bridgeBack_ z:0];
		[self addChild:bridgeRed_ z:1];
		[self addChild:bridge_ z:2];
		[self addChild:cloud_ z:3];
		[self addChild:boy_ z:4];
		[self addChild:girl_ z:5];
		[self addChild:effectCover_ z:6];
		[self addChild:effectInvincible_ z:7];
		[effectCover_ release];
		[effectInvincible_ release];

		[boy_ runAction:[CCHide action]];
		[girl_ runAction:[CCHide action]];

		bridge_.percentage = 0;
		bridge_.type = kCCProgressTimerTypeHorizontalBarLR;
		bridgeRed_.percentage = 0;
		bridgeRed_.type = kCCProgressTimerTypeHorizontalBarLR;

		bridgeBack_.opacity = 80;
		boy_.positionInPixels = startPos_;
		girl_.positionInPixels = endPos_;

		arcLeft_ = g_CfgGameBridgeArcLeft;
		arcRight_ = g_CfgGameBridgeArcRight;
		arcTop_ = g_CfgGameBridgeArcTop;
		arcCenter_.x = 0.0f;
		arcCenter_.y = (arcRight_.x * arcRight_.x - arcTop_.y * arcTop_.y + arcRight_.y * arcRight_.y) / (2 * (arcRight_.y - arcTop_.y));
		bridgeSize_ = g_CfgGameBridgeSize;
	}
	return self;
}

- (void)tickInvincibleOver:(ccTime)Time
{
	[self unschedule:@selector(tickInvincibleOver:)];
	flagInvincible_ = false;
	[effectInvincible_ stopAllActions];
	[effectInvincible_ runAction:[CCHide action]];
}

- (void)tickSuccess:(ccTime)Time
{
	CGPoint _Position;

	_Position = [self GetArcPoint:boy_.positionInPixels.x];
	[boy_ setPositionInPixels:ccp(boy_.positionInPixels.x, _Position.y)];

	_Position = [self GetArcPoint:girl_.positionInPixels.x];
	[girl_ setPositionInPixels:ccp(girl_.positionInPixels.x, _Position.y)];
}

- (void)MissionStart
{
	[boy_ runAction:[CCHide action]];
	[girl_ runAction:[CCHide action]];

	boy_.positionInPixels = startPos_;
	girl_.positionInPixels = endPos_;

	point_ = CMgrGameMission::Instance()->start_bridge_point;

	/*set progress bar
	*/
	bridge_.percentage = point_;
	bridgeRed_.percentage = point_;

	/*clear item flag
	*/
	flagCover_ = false;
	[effectCover_ stopAllActions];
	[effectCover_ runAction:[CCHide action]];

	[self unschedule:@selector(tickInvincibleOver:)];
	flagInvincible_ = false;
	[effectInvincible_ stopAllActions];
	[effectInvincible_ runAction:[CCHide action]];
}

- (void)Pause
{
	[self pauseSchedulerAndActions];
	[bridge_ pauseSchedulerAndActions];
	[bridgeRed_ pauseSchedulerAndActions];
	[boy_ pauseSchedulerAndActions];
	[girl_ pauseSchedulerAndActions];
	[effectCover_ pauseSchedulerAndActions];
	[effectInvincible_ pauseSchedulerAndActions];
}

- (void)Resume
{
	[self resumeSchedulerAndActions];
	[bridge_ resumeSchedulerAndActions];
	[bridgeRed_ resumeSchedulerAndActions];
	[boy_ resumeSchedulerAndActions];
	[girl_ resumeSchedulerAndActions];
	[effectCover_ resumeSchedulerAndActions];
	[effectInvincible_ resumeSchedulerAndActions];
}

- (CGPoint)GetBridgeHeadPosition
{
	return [self GetBridgePositionByX:bridgeSize_.width * bridge_.percentage / 100.0];
}

- (CGPoint)GetBridgePositionByX:(float)X
{
	return ccpAdd(self.positionInPixels, [self GetArcPoint:X - self.positionInPixels.x]);
}

- (CGPoint)GetArcPoint:(float)X
{
	CGPoint _Ret;
	float _R;

	_R = arcTop_.y - arcCenter_.y;
	_Ret.x = X;
	_Ret.y = sqrt(pow(_R, 2) - pow(_Ret.x, 2)) + arcCenter_.y;
	return _Ret;
}

- (void)AddPoint:(int)Point
{
	point_ += Point;
	if (100 < point_)
	{
		point_ = 100;
	}
	if (100 == point_)
	{
		CMgrGameMission::Instance()->EventBridgeSuccess();
	}
	if (bridge_.percentage != point_)
	{
		[self ActionBridgeAdd:BRIDGE_DEFAULT_ACTION_TIME];
	}
}

- (void)DecPoint:(int)Point
{
	// return; /*now you are a god*/

	if (flagCover_)
	{
		flagCover_ = false;
		[effectCover_ stopAllActions];
		[effectCover_ runAction:[CCHide action]];
		return;
	}

	if (flagInvincible_)
	{
		return;
	}

	point_ -= Point;
	if (0 > point_)
	{
		point_ = 0;
	}
	if (bridge_.percentage != point_)
	{
		[self ActionBridgeDec:BRIDGE_DEFAULT_ACTION_TIME];
	}
}

- (void)PlayEffectCover
{
	CCSequence *_Seq;

	_Seq = [CCSequence actions:[CCAnimate actionWithAnimation:uiCreateAnimationObject(&g_CfgGameAniBridgeCover)], [CCHide action], nil];
	[effectCover_ runAction:_Seq];
}

- (void)ActionMissionStart
{
	bridgeBack_.opacity = 0;
	[bridgeBack_ runAction:[CCFadeTo actionWithDuration:MISSION_START_PLAY_TIME opacity:128]];
	[cloud_ runAction:[CCFadeIn actionWithDuration:MISSION_START_PLAY_TIME]];
	[boy_ runAction:[CCFadeIn actionWithDuration:MISSION_START_PLAY_TIME]];
	[girl_ runAction:[CCFadeIn actionWithDuration:MISSION_START_PLAY_TIME]];
	[self ActionBridgeAdd:MISSION_START_PLAY_TIME];
}

- (void)ActionCover
{
	flagCover_ = true;
	[effectCover_ runAction:[CCShow action]];
	[effectCover_ runAction:tcCreateRepeatAnimate(&g_CfgGameAniBridgeCover)];
}

- (void)ActionInvincible:(float)Time
{
	if (flagCover_)
	{
		flagCover_ = false;
		[effectCover_ stopAllActions];
		[effectCover_ runAction:[CCHide action]];
	}

	flagInvincible_ = true;
	[effectInvincible_ runAction:[CCShow action]];
	[effectInvincible_ runAction:tcCreateRepeatAnimate(&g_CfgGameAniBridgeCover)];
	[self schedule:@selector(tickInvincibleOver:) interval:Time];
}

- (void)ActionBridgeAdd:(float)Time
{
	[bridge_ stopAllActions];
	if (bridge_.percentage == 100)
	{
		bridge_.percentage = 99;
	}
	[bridge_ runAction:[CCProgressTo actionWithDuration:1 percent:point_]];
}

- (void)ActionBridgeDec:(float)Time
{
	id _Action;
	CCCallFuncN *_CallBack;
	CCSequence *_Seq;

	[bridge_ stopAllActions];
	[bridgeRed_ stopAllActions];
	if (bridgeRed_.percentage < bridge_.percentage)
	{
		bridgeRed_.percentage = bridge_.percentage;
	}
	bridge_.percentage = point_;
	if (bridgeRed_.percentage == 100)
	{
		bridgeRed_.percentage = 99;
	}
	_Action = [CCProgressTo actionWithDuration:1 percent:point_];
	_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionBirdgeDecOver:)];
	_Seq = [CCSequence actions:_Action, _CallBack, nil];
	[bridgeRed_ runAction:_Seq];
}

- (void)ActionSuccess
{
	CCSequence *_Seq;
	id _ActionBoy, _ActionGirl;
	CCCallFuncN *_CallBackSuccessOver;

	[boy_ runAction:[CCShow action]];
	[girl_ runAction:[CCShow action]];
	_ActionBoy = [action_move_x_in_pixel actionWithDuration:3 position:CGPointZero];
	_ActionGirl = [action_move_x_in_pixel actionWithDuration:3 position:CGPointZero];
	_CallBackSuccessOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionSuccessOver:)];
	_Seq = [CCSequence actions:_ActionBoy, _CallBackSuccessOver, nil];
	[boy_ runAction:_Seq];
	[girl_ runAction:_ActionGirl];
	[self schedule:@selector(tickSuccess:)];
}

- (void)CallBackActionBirdgeDecOver:(id)Sender
{
	if (0 == point_)
	{
		CMgrGameMission::Instance()->EventBridgeFailed();
	}
}

- (void)CallBackActionSuccessOver:(id)Sender
{
	[self unschedule:@selector(tickSuccess:)];
	CMgrGameMission::Instance()->EventBridgeSuccessAniOver();
}

@end